home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / open / open.c < prev   
Encoding:
C/C++ Source or Header  |  1989-06-15  |  5.4 KB  |  253 lines

  1. /*
  2.  * open.c --
  3.  *    Test of the open system call.  This does timing tests or
  4.  *    error condition testing.
  5.  */
  6.  
  7. #include <sprite.h>
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/time.h>
  11. #include <sys/file.h>
  12. #include <sys/stat.h>
  13. #include <option.h>
  14.  
  15. Boolean timing = FALSE;
  16. Boolean errorTest = FALSE;
  17.  
  18. Option optionArray[] = {
  19.     {OPT_TRUE, "t", (char *) &timing, "Do timing test"},
  20.     {OPT_TRUE, "e", (char *) &errorTest, "Do error tests"},
  21. };
  22.  
  23. #define NAME_SIZE    10 * 1024
  24. char name[NAME_SIZE];
  25.  
  26. main(argc, argv)
  27.     int argc;
  28.     char **argv;
  29. {
  30.     argc = Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), 0);
  31.  
  32.     if (timing) {
  33.     DoTiming(argc, argv);
  34.     }
  35.     if (errorTest) {
  36.     DoOpenErrors(argc, argv);
  37.     }
  38. }
  39.  
  40. /*
  41.  * DoTiming --
  42.  *    Time the cost of an open/close pair.
  43.  */
  44. DoTiming(argc, argv)
  45.     int argc;
  46.     char *argv[];
  47. {
  48.     int count, index, i;
  49.     char name[200];
  50.     FILE *f;
  51.     double cost;
  52.     struct timeval before, after;
  53.     struct timezone foo;
  54.  
  55.     if (argc < 2) {
  56.     count = 100;
  57.     } else {
  58.     count = atoi(argv[1]);
  59.     }
  60.     if (argc == 3) {
  61.     sprintf(name, "%s/.FooA", argv[2]);
  62.     }  else {
  63.     strcpy(name, ".FooA");
  64.     }
  65.     index = strlen(name) - 1;
  66.  
  67.     /*
  68.      * Create 20 files in the given directory with names like ".FooA"
  69.      * etc.
  70.      */
  71.  
  72.     for (i = 0; i < 20; i++) {
  73.     name[index] = 'A' + i;
  74.     f = fopen(name, "w");
  75.     if (f == NULL) {
  76.         fprintf(stderr, "Couldn't create %s.\n", name);
  77.         exit(1);
  78.     }
  79.     fclose(f);
  80.     }
  81.  
  82.     /*
  83.      * Time the opens and closes.
  84.      */
  85.  
  86.     gettimeofday(&before, &foo);
  87.     for (i = 0; i < count; i++) {
  88.     name[index] = 'A' + i%20;
  89.     f = fopen(name, "r");
  90.     fclose(f);
  91.     }
  92.     gettimeofday(&after, &foo);
  93.     cost = after.tv_sec - before.tv_sec;
  94.     cost += (after.tv_usec - before.tv_usec)*.000001;
  95.     cost /= count;
  96.     cost *= 1000;
  97.     printf("%d opens and closes at %.2f msec each.\n", count, cost);
  98.  
  99.     /*
  100.      * Remove all the temporary files that were created.
  101.      */
  102.  
  103.     for (count = 0; count < 20; count++) {
  104.     name[index] = 'A' + count;
  105.     unlink(name);
  106.     }
  107. }
  108.  
  109. /*
  110.  * DoOpenErrors --
  111.  *    Attempt to generate all errors associated with open.
  112.  */
  113. DoOpenErrors(argc, argv)
  114.     int argc;
  115.     char *argv[];
  116. {
  117.     printf("Open Error Tests\n");
  118.     printf("    ERROR: indicates failure case did not work\n");
  119.     printf("    otherwise perror() is used to print system error\n\n");
  120.     /*
  121.      * Test bad pathname argument.
  122.      */
  123.     {
  124.     int fd;
  125.     struct stat statb;
  126.  
  127.     if ((fd = open(-1, 0, 0)) >= 0) {
  128.         printf("ERROR: open(-1, 0, 0) succeeded!\n");
  129.         if (fstat(fd, &statb) < 0) {
  130.         perror("but fstat failed");
  131.         } else {
  132.         printf("-1 found file <%d,%d>\n", statb.st_dev, statb.st_ino);
  133.         close(fd);
  134.         }
  135.     } else {
  136.         perror("open(-1, 0, 0) (bad pathname)");
  137.     }
  138.     if ((fd = open(15, 0, 0)) >= 0) {
  139.         printf("ERROR: open(15, 0, 0) succeeded!\n");
  140.         if (fstat(fd, &statb) < 0) {
  141.         perror("but fstat failed");
  142.         } else {
  143.         printf("15 found file <%d,%d>\n", statb.st_dev, statb.st_ino);
  144.         close(fd);
  145.         }
  146.     } else {
  147.         perror("open(15, 0, 0) (bad pathname)");
  148.     }
  149.     }
  150.     /*
  151.      * Test too long a pathname.
  152.      */
  153.     {
  154.     register char *cPtr;
  155.     register int i;
  156.  
  157.     for (cPtr = name, i=0 ; i < NAME_SIZE ; ) {
  158.         *cPtr++ = 'a'; i++ ;
  159.         *cPtr++ = 'a'; i++ ;
  160.         *cPtr++ = 'a'; i++ ;
  161.         *cPtr++ = 'a'; i++ ;
  162.         *cPtr++ = 'a'; i++ ;
  163.         *cPtr++ = 'a'; i++ ;
  164.         *cPtr++ = 'a'; i++ ;
  165.         *cPtr++ = 'a'; i++ ;
  166.         *cPtr++ = 'a'; i++ ;
  167.         *cPtr++ = 'a'; i++ ;
  168.     }
  169.     if (open(name, 0, 0) >= 0) {
  170.         printf("ERROR: open(tooLongName, 0, 0) succeeded!\n");
  171.     } else {
  172.         perror("open(tooLongName, 0, 0)");
  173.     }
  174.     }
  175.     /*
  176.      *  Do various permission checks.  This checks against violations
  177.      *    of the following conditions:
  178.      *        Exclusive open
  179.      *        read permission
  180.      *        write permission
  181.      *        busy file
  182.      *        writing a directory
  183.      */
  184.     {
  185.     char *name = "./FooA";
  186.     int fd, fd2;
  187.  
  188.     /*
  189.      * Test open-unlink-close
  190.      */
  191.     if ((fd = open(name, O_CREAT, 0444)) < 0) {
  192.         perror("Can't create ./FooA");
  193.     } else {
  194.         if ((fd2 = open(name, O_RDONLY)) >= 0) {
  195.         close(fd2);
  196.         } else {
  197.         perror("Can't open readable file");
  198.         }
  199.         unlink(name);
  200.         close(fd);
  201.     }
  202.     /*
  203.      * Test permission checking.
  204.      */
  205.     if ((fd = open(name, O_CREAT, 0)) < 0) {
  206.         perror("Can't create ./FooA");
  207.     } else {
  208.         if ((fd2 = open(name, O_CREAT|O_EXCL, 0)) >= 0) {
  209.         printf("ERROR: exclusive open of existing file succeeded!\n");
  210.         if (chmod(name, 0) < 0) {
  211.             perror("Can't chmod file");
  212.         }
  213.         close(fd2);
  214.         } else {
  215.         perror("open exclusive");
  216.         }
  217.         if (open(name, O_RDONLY) >= 0) {
  218.         printf("ERROR: opened with no read permission!\n");
  219.         } else {
  220.         perror("read when mode == 0");
  221.         }
  222.         chmod(name, 0444);
  223.         if (open(name, O_WRONLY) >= 0) {
  224.         printf("ERROR: opened with no write permission!\n");
  225.         } else {
  226.         perror("write when mode == read only");
  227.         }
  228.         if (open("/sprite/cmds/csh", O_WRONLY) >= 0) {
  229.         printf("ERROR: opened executing program (csh) for writing!\n");
  230.         } else {
  231.         perror("open active file");
  232.         }
  233.         if (open(".", O_WRONLY) >= 0) {
  234.         printf("ERROR: opened directory for writing!\n");
  235.         } else {
  236.         perror("write directory");
  237.         }
  238.         if (unlink(name) < 0) {
  239.         perror("Can't remove test file");
  240.         }
  241.         /*
  242.          * Test closing closed file.
  243.          */
  244.         close(fd);
  245.         if (close(fd) < 0)  {
  246.         perror("close of closed file");
  247.         } else {
  248.         printf("ERROR: closed a closed file descriptor!\n");
  249.         }
  250.     }
  251.     }
  252. }
  253.